<?

$dataup = 0;
$uprun  = true;

/**
* Check the entry
* Special case check
* if the SN entry doesnt duplicated
*
*/
if($_POST["entry"]){

  $Db = new oracleDb;
  $Db->init($_SESSION["ORCL"],
             $_SESSION["OPASS"],
             $_SESSION["SID"],
             _NO_COMMENT);

  if($Db->connect()){

    for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

      /**
      * Check for each master detail
      *
      */
      if($_POST["modelEnt"][$iRow] == '' &&
         $_POST["qtyEnt"][$iRow]   == ''){

        for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

          if($_POST["modelEnt"][$iNext] != '' ||
             $_POST["qtyEnt"][$iNext]    != ''){

            $uprun = false;

            $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

            break;
          }
        }

        if($uprun == false){

          /* out of this loop */
          break;
        }
      }else{

        if($_POST["modelEnt"][$iRow] != '' &&
           $_POST["qtyEnt"][$iRow]   != ''){

          if($_POST["serNumEnt"][$iRow]){

            /**
             * check out the validity
             */
            $retSN  = $CLocal->isSnValid($Misc,
                                         $_POST["qtyEnt"][$iRow],
                                         $_POST["headEnt"][$iRow],
                                         $_POST["lengthEnt"][$iRow],
                                         $_POST["serNumEnt"][$iRow],
                                         $Db,
                                         $SourceDb->fetchValue("srcDbSubDtl"));

          }else{

            $retSN = false;
          }

          if(!$retSN){

            $dataup++;
          }else{

            $uprun = false;

            $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." <br/>
                             Duplicate SN Entry ".$retSN." ");
          }
        }else{

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
        }
      }
    }

    $Db->dc();

  }else{

    $Misc->usrDie(_DB_CONN_ERROR);
  }
}else{

  for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

    /**
    * Check for each master detail
    *
    */
    if($_POST["modelEnt"][$iRow] == '' &&
       $_POST["qtyEnt"][$iRow]   == ''){

      for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

        if($_POST["modelEnt"][$iNext] != '' ||
           $_POST["qtyEnt"][$iNext]    != ''){

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

          break;
        }
      }

      if($uprun == false){

        /* out of this loop */
        break;
      }
    }else{

      if($_POST["modelEnt"][$iRow] != '' &&
         $_POST["qtyEnt"][$iRow]   != ''){

        $dataup++;
      }else{

        $uprun = false;

        $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
      }
    }
  }
}

if($uprun == true && $dataup > 0){

	/**
	* We are good with details
	* continue with the header check
	*
	*/
	if($_POST["trDateEnt"]    != '' ){

		//everything is good
		//open confirmation form
		if(!$_POST["confirm"] && !$_POST["back"]){

			require("entry_upload_confirm.php");
		}elseif($_POST["back"]){

			require("entry_restore_me.php");
		}else{

			/**
			* Prepare to upload
			* modify mode is on
			*
			*/
			if($_SESSION["modify_mode"] == "Y" && $_SESSION["jid"] != ''){

				require("upmodifytodb.php");
			}elseif($_SESSION["modify_mode"] != "Y"){ //if modify mode is off

				require("upentrytodb.php");
			}else{

				$Misc->usrError("Unknown Upload Task");
			}
		}
	}else{

		$Misc->usrError("Error: header incompleted");

		require("entry_restore_me.php");
	}
}

if($uprun == false){

	echo "Restoring...\n";
	$restore  = 0;
	$uprun    = 0;
	$dataup   = 0;

	require("entry_restore_me.php");
}
?>